package com.java2104.controller;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;

//定义拦截器，需要实现HandlerInterceptor接口
public class SystemTimingInterceptor implements HandlerInterceptor {
    private int openTiming = 8;//早晨8点钟开放
    private int closeTiming = 18;//晚上6点钟关闭

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Calendar cal = Calendar.getInstance();//当前系统时间
        int hour = cal.get(Calendar.HOUR_OF_DAY);//获取当前是几点
        if (hour>=openTiming && hour<closeTiming)
            return true;
        else {
            response.sendRedirect("/notInWorkTime.jsp");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public int getOpenTiming() {
        return openTiming;
    }

    public void setOpenTiming(int openTiming) {
        this.openTiming = openTiming;
    }

    public int getCloseTiming() {
        return closeTiming;
    }

    public void setCloseTiming(int closeTiming) {
        this.closeTiming = closeTiming;
    }
}
